package com.zbkj.service.util;


import com.zbkj.common.model.shop.EbGroupPurchaseBusinessHours;
import com.zbkj.common.response.EbGroupBuyShopBusinessHoursResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 星期处理工具类
 */
public class WeekdayTextUtil {
    public static String convertToWeekdays(List<Integer> days) {
        if (days.contains(0)) {
            return "营业时间内可用";
        }

        boolean[] weekdayFlags = new boolean[8]; // 索引1~7对应周一到周日

        for (int day : days) {
            if (day >= 1 && day <= 7) {
                weekdayFlags[day] = true;
            }
        }

        StringBuilder result = new StringBuilder();
        boolean started = false;
        int start = -1;

        for (int i = 1; i <= 7; i++) {
            if (weekdayFlags[i]) {
                if (!started) {
                    start = i;
                    started = true;
                }
            } else {
                if (started) {
                    if (start == i - 1) {
                        // 单个日期
                        result.append(getWeekday(start)).append(", ");
                    } else {
                        // 区间
                        result.append(getWeekday(start)).append("至").append(getWeekday(i - 1)).append(", ");
                    }
                    started = false;
                }
            }
        }

        // 处理结尾可能未闭合的区间
        if (started) {
            if (start == 7) {
                result.append(getWeekday(start)).append(", ");
            } else {
                result.append(getWeekday(start)).append("至").append(getWeekday(7)).append(", ");
            }
        }

        String finalResult = result.toString().trim();
        if (finalResult.endsWith(",")) {
            finalResult = finalResult.substring(0, finalResult.length() - 1);
        }
        return finalResult;
    }

    private static String getWeekday(int day) {
        switch (day) {
            case 1:
                return "周一";
            case 2:
                return "周二";
            case 3:
                return "周三";
            case 4:
                return "周四";
            case 5:
                return "周五";
            case 6:
                return "周六";
            case 7:
                return "周日";
            default:
                return "";
        }
    }


    private static void appendRange(StringBuilder result, int start, int end) {
        if (start == end) {
            result.append(getWeekday(start)).append(", ");
        } else {
            result.append(getWeekday(start)).append("至").append(getWeekday(end)).append(", ");
        }
    }


    /**
     * 获取营业信息
     *
     * @param hoursList
     * @return
     */
    public String getGroupPurchaseHoursText(List<EbGroupPurchaseBusinessHours> businessHoursList) {
        Map<String, List<EbGroupPurchaseBusinessHours>> map = new HashMap<>();
        for (EbGroupPurchaseBusinessHours hoursResponse : businessHoursList) {

            String key = hoursResponse.getOpenTime().toString() + "-" + hoursResponse.getCloseTime().toString();

            //组装map
            List<EbGroupPurchaseBusinessHours> groupPurchaseBusinessHoursList = map.get(key);
            if (groupPurchaseBusinessHoursList == null) {
                groupPurchaseBusinessHoursList = new ArrayList<>();
            }
            groupPurchaseBusinessHoursList.add(hoursResponse);
            map.put(key, groupPurchaseBusinessHoursList);
        }
        StringBuilder stringBuilder = null;
        for (String key : map.keySet()) {
            if (stringBuilder == null) {
                stringBuilder = new StringBuilder();
            } else {
                stringBuilder.append(" 与 ");
            }
            List<EbGroupPurchaseBusinessHours> hoursList = map.get(key);
            List<Integer> list = new ArrayList<>();
            String time = "";
            for (EbGroupPurchaseBusinessHours hoursResponse :
                    hoursList) {
                list.add(hoursResponse.getDayOfWeek());
                time = hoursResponse.getOpenTime() + "-" +
                        hoursResponse.getCloseTime();
            }
            stringBuilder.append(convertToWeekdays(list));
            stringBuilder.append(" ");
            stringBuilder.append(time);
        }

        return stringBuilder.toString();
    }

    /**
     * 获取营业信息
     *
     * @param businessHoursList
     * @return
     */
    public String getShopHoursText(List<EbGroupBuyShopBusinessHoursResponse>
                                           businessHoursList) {
        Map<String, List<EbGroupBuyShopBusinessHoursResponse>> map = new HashMap<>();
        for (EbGroupBuyShopBusinessHoursResponse hoursResponse : businessHoursList) {

            String key = hoursResponse.getOpenTime().toString() + "-" + hoursResponse.getCloseTime().toString();

            //组装map
            List<EbGroupBuyShopBusinessHoursResponse> groupPurchaseBusinessHoursList = map.get(key);
            if (groupPurchaseBusinessHoursList == null) {
                groupPurchaseBusinessHoursList = new ArrayList<>();
            }
            groupPurchaseBusinessHoursList.add(hoursResponse);
            map.put(key, groupPurchaseBusinessHoursList);
        }
        StringBuilder stringBuilder = null;
        for (String key : map.keySet()) {
            if (stringBuilder == null) {
                stringBuilder = new StringBuilder();
            } else {
                stringBuilder.append(" 与 ");
            }
            List<EbGroupBuyShopBusinessHoursResponse> hoursList = map.get(key);
            List<Integer> list = new ArrayList<>();
            String time = "";
            for (EbGroupBuyShopBusinessHoursResponse hoursResponse :
                    hoursList) {
                list.add(hoursResponse.getDayOfWeek());
                time = hoursResponse.getOpenTime() + "-" +
                        hoursResponse.getCloseTime();
            }
            stringBuilder.append(convertToWeekdays(list));
            stringBuilder.append(" ");
            stringBuilder.append(time);
        }
        return stringBuilder.toString();
    }
}